//
//  HNMapGrid.m
//  HeroNuts
//
//  Created by eyexing on 11-5-16.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "HNMapGrid.h"
#import "HNLogicPosition.h"


@implementation HNMapGrid
//@synthesize batch = batch_;
//@synthesize selected = selected_;

- (void)dealloc 
{    
	
    [super dealloc];
}

- (id) initWithParent: (CCLayer *)layer
{
    if ( (self = [super init]) ) 
    {
        batch_ = [CCSpriteBatchNode batchNodeWithFile:@"battle_3.png" capacity:30];
        [layer addChild:batch_];
        selected_ = [CCSprite spriteWithFile:@"battle_3.png"];
		[layer addChild:selected_];
        selected_.anchorPoint = ccp(0,1);
        selected_.position = ccp(0,0);
        selected_.visible = NO;
        
	}
	return self;
}

- (CGPoint) changeToRealPosition: (CGPoint)position
{
    CGPoint distance = [HNLogicPosition distanceInPixelsFrom:ccp(0,0) to:position];
    CGPoint result = ccpAdd(ccp(80,93), distance);
    
    return result;
}

- (void) highlightGridwithArray: (NSArray *)gridArray
{
    //self.highlightGrid = [[NSMutableArray alloc] initWithArray:gridArray];
    for (int i = 0; i < [gridArray count]; i++) 
    {
        CCSprite *highlightGrid = [CCSprite spriteWithFile:@"battle_3.png"];
        CGPoint point = [[gridArray objectAtIndex:i] position];
		
		CCLOG(@"%f,%f",point.x,point.y);
		
		highlightGrid.anchorPoint = ccp(0,1);
        highlightGrid.position = [self changeToRealPosition:point];
        [batch_ addChild: highlightGrid];
    }
}

- (void) revertHighlight
{
    [batch_ removeAllChildrenWithCleanup:YES];
}


#pragma mark Delegate
- (void)hnTouchBeganAtPosition:(CGPoint)position
{
    selected_.visible = YES;
    selected_.position = [self changeToRealPosition:position];
}

- (void)hnTouchMovedToPosition:(CGPoint)position
{

	selected_.visible = YES;
	selected_.position = [self changeToRealPosition:position];
    
}

- (void)hnTouchEndedAtPosition:(CGPoint)position
{

	selected_.visible = NO;

}



@end
